#pragma once
#include "imtPublic.h"
#include "imtInputer.h"
#include "imtGameScene.h"
#include "imtObjectPicker.h"
#include "imdManuplator.h"
#include "imtAttribSet.h"
#include "imdResImporter.h"

namespace iMate {

	class RttObject;

	class DsgPlayer : public NormalAlloc
	{
	public:
		DsgPlayer(Inputer* inputer, GuiSystem* guiSystem)
			: mGuiSystem(guiSystem), mInputer(inputer), 
			mScene(0), mCamera(0), mSelectedObject(0), mSelectedNode(0),
			mTranslator(Manuplator::MT_Translate), mRotator(Manuplator::MT_Rotate),
			mScalor(Manuplator::MT_Scale), mGlobalIndex(0)
		{}

		void setCurrentScene(GameScene* scene);
		void setCurrentCamera(Camera* camera);

		void update(Real timeElapsed);

		void onKeyPress(Inputer* sender, KeyPressEvent& evt);
		void onWipe(Inputer* sender, WipeEvent& evt);
		void onTouch(Inputer* sender, TouchEvent& evt);
		void onMousePress(Inputer* sender, PressEvent& evt);
		void activeManupulator(Manuplator::ManupType manupType);
		Manuplator* getActiveManupulator();
		Manuplator* getVisibleManupulator();
		void setCurrentManupulatorVisible(bool v);
		void create();
		void resetManupulators();

	public:
		enum DefaultCameraView
		{
			DCV_Home,
			DCV_Left,
			DCV_Up,
			DCV_Back
		};
		void setDefaultCameraView(DefaultCameraView view);

	protected:
		void setupGuiEventHandlers();

		bool _handleManupulatorRadio(const CEGUI::EventArgs& args);
		bool _handleViewRadio(const CEGUI::EventArgs& args);
		bool _handleDisplayRadio(const CEGUI::EventArgs& args);
		bool _handleBoundBoxChecker(const CEGUI::EventArgs& args);
		bool _handleFileMenuItem(const CEGUI::EventArgs& args);
		bool _handleObjectListItem(const CEGUI::EventArgs& args);
		bool _handleObjectListBoxKey(const CEGUI::EventArgs& args);
		bool _handleButtonClicked(const CEGUI::EventArgs& args);
		bool _handleRttObjSelect(const CEGUI::EventArgs& args);
		void _handleImportRtt(ResourceImporter* importer, std::vector<String>& objs);

		void clearObjectListbox();
		void rebuildOjectListboxFromScene();
		void clearCurrentSceneObjects();
		bool selectSceneObject(const String& name);
		void unselectCurrent();
		void deleteObject(const String& name);

		RttObject* createRtt(CEGUI::Window* window, String& meshName);
		void clearPreviews(bool deleteRtt);
		bool meshPreviewExist(const String& meshName);

		void addEntity(const String& meshName);

	protected:
		Camera*      mCamera;
		GuiSystem*   mGuiSystem;
		GameScene*   mScene;
		ObjectPicker mPicker;
		Inputer*     mInputer;
		ISceneObject* mSelectedObject;
		Ogre::SceneNode* mSelectedNode;
		Manuplator   mTranslator;
		Manuplator   mRotator;
		Manuplator   mScalor;
		AttribSetShower  mAttribArea;
		ResourceImporter mResourceImporter;
		std::map<CEGUI::Window*, String> mMeshObjects;
		uint         mGlobalIndex;
	};

	class DsgScene : public GameScene
	{
		DsgPlayer* mPlayer;
		Camera* mCamera;
	public:
		DsgScene(Inputer* inputer, GuiSystem* guiSystem,
			SceneManager* sceneManager,RenderWindow* renderWindow, Camera* camera);

		virtual bool createScene(DataStream* stream);
		virtual void destroyScene();
		virtual bool update(const Real timeDelta);
		DsgPlayer* getPlayer() {return mPlayer;}
	};


}//iMate